package router

import (
	"iris-starter/bootstrap"
	"iris-starter/controllers/api"
	"iris-starter/controllers/web"
	"iris-starter/middleware"
	"iris-starter/service"

	"github.com/casbin/casbin/v2"
	casbinMiddleware "github.com/iris-contrib/middleware/casbin"
	"github.com/kataras/iris/v12/mvc"
)

var enforcer, _ = casbin.NewEnforcer("./config/model.conf", "./config/policy.csv")

func ConfigureRouter(b *bootstrap.Bootstrapper) {
	v1 := b.Party("/v1/api")
	{
		auth := mvc.New(v1.Party("/auth"))
		auth.Handle(new(api.AuthController))

		mvc.Configure(v1.Party("/admin_user"), func(m *mvc.Application) {
			m.Router.Use(middleware.JwtVerify())
			cm := casbinMiddleware.New(enforcer)
			m.Router.Use(cm.ServeHTTP)
			m.Register(service.NewAdminUserSrv())
			m.Handle(new(api.AdminUserController))
		})
	}
}

func ConfigureWebRouter(b *bootstrap.Bootstrapper) {
	mvc.Configure(b.Party("/wap"), func(m *mvc.Application) {
		m.Router.Use(b.Sessions.Handler())
		m.Handle(new(web.HomeController))
	})
}
